package com.xupla.helloworld;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class XuplaErrorHandler {
	private static Activity parent = null;
	private static boolean debug = false;
	private static int errCnt = -1;
	
	public XuplaErrorHandler( boolean isDebug, Activity ac )
	{		
		parent = ac;
		debug = isDebug;
		errCnt = 0;
	}
		
	public void handleError( String message, Activity initiator ) throws Exception
    {				
		Log.i( "Xupla Error Handler:", "Start Error Handling" );
		if( debug | true )
		{
			handleError( message, 3 );
		}
    }
	
	private void handleError( String message, int severity ) throws Exception
	{
    	NotificationManager notMng = (NotificationManager) parent.getSystemService(Context.NOTIFICATION_SERVICE);
        
    	//PendingIntent to launch our activity if the user selects it 
		PendingIntent i = PendingIntent.getActivity(parent, 0,
		                            new Intent(parent, XuplaErrorHandlerActivity.class),
		                                              0);
		
		int ico = -1;
		
		switch (severity)
		{
		case 0:
			// do nothing		
		case 1:
			ico = R.drawable.ic_atari_1_bomb;
		case 2:
			ico = R.drawable.ic_atari_2_bombs;
		case 3:
			ico = R.drawable.ic_atari_3_bombs;
		
		}
		
	
				
		
				
				
		
		// Create notification
		Notification notifyObj = new Notification.Builder( parent )
    	        .setContentTitle( "Oops, error..." )
    	        .setContentText( message ).setSmallIcon( ico )    	        
    	        .setContentIntent(i).getNotification(); //.build when 4.0 is outdated...
    		
    			/*.addAction(R.drawable.icon, "Call", pIntent) // use when necessary
    	        .addAction(R.drawable.icon, "More", pIntent)
    	        .addAction(R.drawable.icon, "And more", pIntent).build();*/    	            	        		

		//Value indicates the current number of events represented by the notification
		//notifyObj.number=++count;
		//Set default vibration
		notifyObj.defaults |= Notification.DEFAULT_VIBRATE;
		//Set default notification sound
		notifyObj.defaults |= Notification.DEFAULT_SOUND;
		//Clear the status notification when the user selects it
		notifyObj.flags|=Notification.FLAG_AUTO_CANCEL;    
		//Send notification
		errCnt++;
		notMng.notify(errCnt, notifyObj);
		
		// end parent as its erroneous
		//parent.finish();	// does this work?!?			
	}
}
